<?php

namespace app\common\command;
use app\common\model\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class UserSettlement extends Command
{
    protected function configure()
    {
        $this->setName('userSettlement')->setDescription('利息结算');
    }

    protected function execute(Input $input, Output $output)
    {
        $userModel = new User();
//        $userData = $userModel->where('wallet_end_time','>',0)->where('wallet_end_time','<',time())->select();
        $userData = $userModel->where('wallet_end_time','>',0)->select();

        if($userData){
            //年利率÷365天×日金额＝日息
            $userFinanceModel = new \app\common\model\UserFinance();
            $res = false;
            Db::startTrans();
            try {
                foreach($userData as $k=>$v){
                    $interest = $userFinanceModel->where('user_id', $v->id)->where('status','=',0)->sum('interest');
                    if($interest > 0){
                        //获取当前用户总利息
                        controller('addons\wanlshop\library\WanlPay\WanlPay')->scoreLog(+$interest, $v->id, '每月分红', 'day_fh');
                        $res = $userFinanceModel->where('user_id', $v->id)->where('status','=',0)->update(['status' => 1,'updatetime'=>time()]);
                    }
                }
                if($res){
                    Db::commit();
                    echo '更新成功';
                }else{
                    Db::rollback();
                    echo '无数据';
                }
            }catch (\Throwable $e){
                Db::rollback();
                echo $e->getMessage();
            }
        }else{
            echo '无满足用户';
        }
    }
}
